﻿#region USING STATEMENTS
using System;
using Microsoft.Xna.Framework;
using Fusion.Objects;
using Fusion.Management;
#endregion

#region CODE BODY
namespace Fusion.Interface
{
    public class Button : BaseControl
    {
        //////////////////////////////////////////////////
        // Data Members
        //////////////////////////////////////////////////
        Sprite BtnSprite;
        string faceText;
        Color foreground;

        //////////////////////////////////////////////////
        // Member Properties
        //////////////////////////////////////////////////
        public Vector2 Position
        {
            get { return BtnSprite.Position; }
            set { BtnSprite.Position = value; }
        }
        public String FaceText
        {
            get { return faceText; }
            set { faceText = value; }
        }
        public Color Foreground
        {
            get { return foreground; }
            set { foreground = value; }
        }
        public Single X
        {
            get { return BtnSprite.X; }
            set { BtnSprite.X = value; }
        }
        public Single Y
        {
            get { return BtnSprite.Y; }
            set { BtnSprite.Y = value; }
        }

        //////////////////////////////////////////////////
        // Constructors
        //////////////////////////////////////////////////
        public Button(String ButtonImage)
            : base()
        {
            BtnSprite = new Sprite(ButtonImage);
        }
        public Button(String ButtonImage, String Text)
            : base()
        {
            BtnSprite = new Sprite(ButtonImage);
            FaceText = Text;
            Foreground = Color.White;
        }
        public Button(String ButtonImage, String Text, Color TextForeground)
            : base()
        {
            BtnSprite = new Sprite(ButtonImage);
            FaceText = Text;
            Foreground = TextForeground;
        }

        //////////////////////////////////////////////////
        // BaseControl Methods
        //////////////////////////////////////////////////
        public override void Update(Single Delta)
        {
            if (BtnSprite.BoxCollider.BoundRect.Contains(FInput.MousePos()))
            {
                // True
                if (FInput.ButtonDown(MouseButton.Left))
                    ExecuteMouseDown();

                if (FInput.ButtonPressed(MouseButton.Left))
                    ExecuteControlPressed();

                ExecuteMouseMove();

                BtnSprite.Overlay = Color.White;
            }
            else
            {
                // False
                BtnSprite.Overlay = Color.Silver;
            }
        }
        public override void Render(Single Delta)
        {
            BtnSprite.Render(Delta);

            if (FaceText != null && FaceText.Length > 0)
            {
                Vector2 TextPos = Position;
                Vector2 TextSize = FCanvas.DefFont.MeasureString(FaceText);

                TextPos.X = Position.X + (BtnSprite.BoxCollider.BoundRect.Width >> 1) - (TextSize.X * 1f) * 0.5f;
                TextPos.Y = Position.Y + (BtnSprite.BoxCollider.BoundRect.Height >> 1) - (TextSize.Y * 1f) * 0.5f;

                FCanvas.DrawString(FaceText, TextPos, Foreground, 0f, Vector2.Zero, 1f, Microsoft.Xna.Framework.Graphics.SpriteEffects.None, 1f);
            }
        }
        public override void Dispose()
        {
            BtnSprite.Dispose();
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////